#!/usr/bin/env python

import XenAPI
import XenAPIPlugin
import sys
import urllib2
import base64
from xml.dom.minidom import parse

path 	= "/root/PrivacyCA/Repository/txtrepo/reArch/"

cert    = path + "x509.cert"
xmlfile = path + "xenaik.xml"
aikblob = path + "outAikBlob"
pubkey  = path + "xen.pub"
aikpub  = path + "outAik.key"

tempxml = "/tmp/out.xml"

##Create an XML with
## certifciate, public key,and aikblob
def gen_out_xml():
    dom = parse(xmlfile)
    root = dom.getElementsByTagName("xentxt:xentxtdata")
    xencert = dom.getElementsByTagName('xentxt:xencert')[0]
    xenpub  = dom.getElementsByTagName("xentxt:xenpubkey")[0]
    blob  = dom.getElementsByTagName("xentxt:aikblob")[0]
    aikpubkey  = dom.getElementsByTagName("xentxt:aikpub")[0]

    f= open (cert)
    xencert.firstChild.nodeValue = base64.b64encode(f.read())
    f.close()

    f = open(pubkey)
    xenpub.firstChild.nodeValue =  base64.b64encode(f.read())
    f.close()

    f = open(aikblob)
    blob.firstChild.nodeValue = base64.b64encode(f.read())
    f.close()

    f = open(aikpub)
    aikpubkey.firstChild.nodeValue = base64.b64encode(f.read())
    f.close()

    f = open(tempxml,"w");
    f.write(dom.toxml())
    f.close()

def main(session, args):
    try:
        gen_out_xml()
        f = open(tempxml,"r")
        data = f.read()
        return data
    except:
        raise

if __name__ == "__main__":
    XenAPIPlugin.dispatch({"main":main})
    #main(None,None)
